<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>

<div id="page-content">
    <div class="row">

        <div class="col-lg-12">
            <div class="panel">
                <div class="panel-heading">
                    <h3 class="panel-title">日程安排</h3>
                </div>
                <div class="panel-body">
                    <div class="form-inline">
                        <div class="row">
                            <div class="col-sm-6 table-toolbar-left">
                                <btn id="btn_add_schedule" class="btn btn-primary btn-sm">
                                    <i class="ti-plus"> </i> 新建日程安排
                                </btn>
                                <%--<btn id="btn_add_schedule" class="btn btn-primary btn-sm">--%>
                                    <%--<i class="ti-close"> </i> 日程取消--%>
                                <%--</btn>--%>
                                <%--<btn id="btn_add_schedule" class="btn btn-primary btn-sm">--%>
                                    <%--<i class="ti-pencil"> </i> 日程修改--%>
                                <%--</btn>--%>
                            </div>
                            <div class="col-sm-6 table-toolbar-right">
                                <span id="spanCatalog" style="display: none">1</span>
                                <div class="btn-group right">
                                    <button id="btnAllSchedules" class="btn btn-success">所有日程</button>
                                    <button id="btnMySchedules" class="btn btn-default">与我有关</button>
                                </div>
                            </div>
                        </div>
                    </div>
                    <!-- Calendar placeholder-->
                    <!-- ============================================ -->
                    <div id='calendar'></div>
                    <!-- ============================================ -->
                </div>
            </div>
        </div>

    </div>
</div>

<script>

    var dialogModal;
    $(function(){

// Initialize the calendar
        // -----------------------------------------------------------------
        $('#calendar').fullCalendar({
            header: {
                left: 'prev,next',
                center: 'title',
                right: 'month,agendaWeek,agendaDay'
            },
            monthNames: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
            monthNamesShort: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
            dayNames: ["周日", "周一", "周二", "周三", "周四", "周五", "周六"],
            dayNamesShort: ["周日", "周一", "周二", "周三", "周四", "周五", "周六"],
            today: ["今天"],
            firstDay: 1,
            buttonText: {
                today: '今天',
                month: '月',
                week: '周',
                day: '日',
                prev: '上一周期',
                next: '下一周期'
            },
            allDayText: '全天',
            eventLimit: true, // allow "more" link when too many events
            allDayDefault: false,
            editable: false,
            droppable: false, // this allows things to be dropped onto the calendar
            currentTimezone: 'Asia/Shanghai',
            timeFormat: 'H:mm',
            eventSources: [
                function(start, end, timezone, callback) {
                    var catalog = parseInt($("#spanCatalog").text());
                    $.ajax({
                        url: '<%=path%>/activiti/schedule/list',
                        cache: false,
                        dataType: 'json',
                        data: {
                            start: start.format(),
                            end: end.format(),
                            catalog: catalog
                        },
                        success: function(data) {
                            var rows = $(data.dataRows);
                            if(rows.length>0){
                                var events = [];
                                rows.each(function(i,item) {
                                    events.push({
                                        className: item.className,
                                        title: item.name + "：" + item.subject,
                                        start: item.start,
                                        end: item.end,
                                        id: item.id,
                                        stype: "schedule"
                                    });
                                });
                                callback(events);
                            }
                        },
                        error:function() {
                            __toastr_error('请求数据出错');
                        }
                    });
                },
                function(start, end, timezone, callback) {
                    var catalog = parseInt($("#spanCatalog").text());
                    $.ajax({
                        url: '<%=path%>/activiti/meeting/list',
                        cache: false,
                        dataType: 'json',
                        data: {
                            start: start.format(),
                            end: end.format(),
                            catalog: catalog,
                            source: 1
                        },
                        success: function(data) {
                            var rows = $(data.dataRows);
                            if(rows.length>0){
                                var events = [];
                                rows.each(function(i,item) {
                                    events.push({
                                        className: item.className,
                                        title: item.name + "：" + item.subject,
                                        start: item.start,
                                        end: item.end,
                                        id: item.id,
                                        stype: "meeting"
                                    });
                                });
                                callback(events);
                            }
                        },
                        error:function() {
                            __toastr_error('请求数据出错');
                        }
                    });
                }
            ],
            eventClick : function( event ){
                //do something here...
                console.log('eventClick中选中Event的id属性值为：', event.id);
                fn_schedule_read(event.id, event.stype);
            },
            eventMouseover : function( event ) {
                // do something here...
                // console.log('eventMouseover被执行，选中Event的title属性值为：', event.title);
            }
        });

    });

    function fn_schedule_read(id,stype) {
        var url = '<%=path%>/activiti/'+stype+'/read/'+id;
        dialogModal = __open_dialog_form_read('日程安排', url);
    }

    $("#btn_add_schedule").click(function(){
        loadURL('<%=path%>/activiti/schedule/create', $('#page-content'));
    });

    $("#btnMySchedules").click(function(){
        $("#spanCatalog").text("2");
        $(this).removeClass("btn-default").addClass("btn-success");
        $("#btnAllSchedules").removeClass("btn-success").addClass("btn-default");
        $('#calendar').fullCalendar('refetchEvents');
    });

    $("#btnAllSchedules").click(function(){
        $("#spanCatalog").text("1");
        $(this).removeClass("btn-default").addClass("btn-success");
        $("#btnMySchedules").removeClass("btn-success").addClass("btn-default");
        $('#calendar').fullCalendar('refetchEvents');
    });

</script>